/**
 *
 * Copyright by Samuel Castro & Joaquim Fonseca, 2011
 * All Rights Reserved
 *
 * NOTES:
 * Please do not redistribute this plugin without my permission
 * Plugin made for private use only
 *
 */
package com.SySammy.SdBank;

import com.SySammy.SdBank.Prop.BankProperties;
import com.SySammy.SdBountyHunter.sdLaw;
import com.nijiko.coelho.iConomy.iConomy;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;

/**
 *
 * @author Sammy
 */
public class LoanRun implements Runnable {

    private sdLaw CE = new sdLaw();
    private sdB plugin;
    private String s;

    public LoanRun(String s, sdB plugin) {
        this.s = s;
        this.plugin = plugin;
    }

    @Override
    public void run() {
        // try {
        if (plugin.getBank().getRepaid(s) <= 0) {
            plugin.getServer().getScheduler().cancelTask(plugin.getBank().getScheduleRepId(s));
            plugin.getServer().getPlayer(s).sendMessage(ChatColor.DARK_AQUA + "You have repayed your debt in time, have a nice day");
            plugin.getBank().Remove(s);
            return;
        } else {
            if (iConomy.getBank().getAccount(s).hasEnough(plugin.getBank().getRepaid(s))) {
                iConomy.getBank().getAccount(s).subtract(plugin.getBank().getRepaid(s));
                Player ply = plugin.getServer().getPlayer(s);
                plugin.getServer().getScheduler().cancelTask(plugin.getBank().getScheduleRepId(s));
                plugin.getBank().Remove(s);
                ply.sendMessage(ChatColor.RED + "Loan time ended, payment taken from your account.");
                return;
            } else if (iConomy.getBank().getAccount(s).getBalance() < plugin.getBank().getRepaid(s)) {
                iConomy.getBank().getAccount(s).setBalance(0);
                if (!CE.HasPrison()) {
                    System.out.println("*****[WARNING] MainCity doesn't have a prison, BankLoans will not make bounties*****");
                    return;
                }
                CE.MakeBounty(s, BankProperties.Prisonowner, 1000, " Didn't repayed bank debt");
                plugin.getServer().broadcastMessage(ChatColor.DARK_AQUA + "A " + ChatColor.GOLD + CE.GetBounty(s) + " " + iConomy.getBank().getCurrency() + ChatColor.DARK_AQUA + " bounty was started on " + ChatColor.GOLD + s + "'s " + ChatColor.DARK_AQUA + "head.");
                plugin.getServer().broadcastMessage(ChatColor.DARK_AQUA + "Reason: " + ChatColor.GOLD + CE.GetReason(s));
                plugin.getServer().getScheduler().cancelTask(plugin.getBank().getScheduleRepId(s));

                Player ply = plugin.getServer().getPlayer(s);
                ply.sendMessage(ChatColor.RED + "====================== WARNING ======================\n");
                ply.sendMessage(ChatColor.RED + " You haven't repayed your debt, legal measures were applied.\n");
                ply.sendMessage(ChatColor.RED + "====================== WARNING ======================");
                return;
            }
        }
        // } catch (Exception e) {
        // }
    }
}
